package com.ruoyi.web.controller.subsidy;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Arrays;
import java.util.concurrent.TimeUnit;

import cn.dev33.satoken.annotation.SaIgnore;
import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.helper.LoginHelper;
import com.ruoyi.subsidy.domain.bo.SysSubsidyMachineBo;
import com.ruoyi.subsidy.domain.bo.SysSubsidyPlantingBo;
import com.ruoyi.subsidy.domain.bo.SysmachingBaseModelBo;
import com.ruoyi.subsidy.service.ISysPlantingPriceService;
import com.ruoyi.subsidy.service.ISysSubsidyMachineService;
import com.ruoyi.subsidy.service.ISysSubsidyMaterialService;
import com.ruoyi.system.domain.SysSubsidyMaterial;
import com.ruoyi.system.domain.bo.SysSubsidyMaterialBo;
import com.ruoyi.system.domain.vo.SysSubsidyMaterialVo;
import lombok.RequiredArgsConstructor;

import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.*;

import cn.dev33.satoken.annotation.SaCheckPermission;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.validate.AddGroup;
import com.ruoyi.common.core.validate.EditGroup;
import com.ruoyi.common.core.validate.QueryGroup;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 农机补贴申请
 *
 * @author nbacheng
 * @date 2025-08-07
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/subsidy/subsidyApplication")
public class SysSubsidyApplicationController extends BaseController {

    //private final iSysSubsidyMachineService iSysSubsidyMachineService;


    private final ISysSubsidyMachineService iSysSubsidyMachineService;

    private final ISysSubsidyMaterialService iSysSubsidyMaterialService;




    /**
     * 查询农机补贴申请列表
     */

    /**
     * 查询农机补贴申请列表
     */
    // @SaCheckPermission("subsidy:subsidyApplication:list")
    @GetMapping("/listPage")
    public TableDataInfo<SysSubsidyMachineBo> list(SysSubsidyMachineBo bo, PageQuery pageQuery) {
        return iSysSubsidyMachineService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出农机补贴申请列表
     */
    // @SaCheckPermission("subsidy:subsidyApplication:export")
    @Log(title = "补贴申请", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(SysSubsidyMachineBo bo, HttpServletResponse response) {
        List<SysSubsidyMachineBo> list = iSysSubsidyMachineService.queryList(bo);
        ExcelUtil.exportExcel(list, "补贴申请", SysSubsidyMachineBo.class, response);
    }

    /**
     * 查询农机补贴申请列表
     */
    // @SaCheckPermission("subsidy:subsidyApplication:list")
    @GetMapping("/list")
    public List<SysSubsidyMachineBo> list(SysSubsidyMachineBo bo) {
        return iSysSubsidyMachineService.queryList(bo);
    }

    /**
     * 查询农机补贴申请列表
     */
    // @SaCheckPermission("subsidy:subsidyApplication:list")
    @GetMapping("/historyOne")
    public SysSubsidyMachineBo historyOne(SysSubsidyMachineBo bo) {
        SysSubsidyMachineBo bores=new SysSubsidyMachineBo();
        List<SysSubsidyMachineBo> list=iSysSubsidyMachineService.queryList(bo);
        //list=iSysSubsidyMachineService.queryList(bo);
        if (CollectionUtils.isNotEmpty(list)){
            bores=list.get(0);
        }
        return bores;
    }



    /**
     * 获取农机补贴申请详细信息
     *
     * @param id 主键
     */
    // @SaCheckPermission("subsidy:subsidyApplication:query")
    @GetMapping("/{id}")
    public R<SysSubsidyMachineBo> getInfo(@NotNull(message = "主键不能为空")
                                          @PathVariable Long id) {
        SysSubsidyMachineBo bo = iSysSubsidyMachineService.queryById(id);
        SysSubsidyMaterialBo materialBo = new SysSubsidyMaterialBo();
        materialBo.setAppNum(bo.getAppNum());
        List<SysSubsidyMaterialVo> list = iSysSubsidyMaterialService.queryList(materialBo);
        List<SysSubsidyMaterialBo> listbo = new ArrayList<>();
        list.forEach(e -> {
            SysSubsidyMaterialBo add = BeanUtil.toBean(e, SysSubsidyMaterialBo.class);
            listbo.add(add);
        });
        bo.setListMateria(listbo);
        return R.ok(bo);
    }


    /**
     * 查询农业补贴申请列表
     */
    // @SaCheckPermission("subsidy:subsidyApplication:list")
    @GetMapping("/listMachingModel")
    public List<String> listMachingModel(SysSubsidyMachineBo bo) throws InterruptedException {
       // Thread.sleep(120000); // 等待2分钟
        return iSysSubsidyMachineService.listMachingModel(bo);
    }

    /**
     * 查询农业补贴申请列表
     */
    // @SaCheckPermission("subsidy:subsidyApplication:list")
    @GetMapping("/listMachingByName")
    public List<SysmachingBaseModelBo> listMachingByName(SysSubsidyMachineBo bo) throws InterruptedException {
        // Thread.sleep(120000); // 等待2分钟
        return iSysSubsidyMachineService.listMachingByName(bo);
    }



    /**
     * 新增补贴申请
     @SaCheckPermission("subsidy:subsidyApplication:add")
     @Log(title = "补贴申请", businessType = BusinessType.INSERT)
     @RepeatSubmit()
     @PostMapping() public R<Void> add(@Validated(AddGroup.class) @RequestBody SysSubsidyMachineBo bo) {
     return toAjax(iSysSubsidyMachineService.insertByBo(bo));
     }
     */

    /**
     * 申请农机补贴
     */
    // @SaCheckPermission("subsidy:subsidyApplication:add")
    @Log(title = "申请农机补贴", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping("/submitnongji")
    public R<Object> submitnongji(@RequestBody SysSubsidyMachineBo bo) {
        //List<SysSubsidyMaterialVo> listMateria=new ArrayList<SysSubsidyMaterialVo>();
        return R.ok((Object) iSysSubsidyMachineService.insertByBo(bo));
        //List<SysSubsidyMaterialVo> listMateria=new ArrayList<SysSubsidyMaterialVo>();


    }

    // @SaCheckPermission("subsidy:subsidyApplication:add")
    @Log(title = "单个驳回接口", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping("/rejectOne")
    public R<Object> RejectOne(@RequestBody SysSubsidyMachineBo bo) {
        SysSubsidyMachineBo boo = iSysSubsidyMachineService.queryList(bo).get(0);
        LoginUser loginUser = LoginHelper.getLoginUser();
        String key = loginUser.getRoles().get(0).getRoleKey();
        /*if ("districtNongji".equals(key)) {
            bo.setNode(Integer.valueOf(boo.getSubmitNode()));
            bo.setStatus("异常");
            bo.setNodeName(boo.getSubmitBy());
        }*/
        if ("districtNongji".equals(key)) {
            bo.setNode(2);
            bo.setStatus("异常");
            bo.setNodeName(boo.getTownName());
        }
        if ("townNongji".equals(key)) {
            bo.setNode(1);
            bo.setStatus("异常");
            bo.setNodeName(boo.getVillageName());
        }
        // bo.setProccResult("驳回");
        // bo.setStatus("已处理");
        Boolean bo1 = iSysSubsidyMachineService.updateByMachineBo(bo);
        return R.ok();
    }

    // @SaCheckPermission("subsidy:subsidyApplication:add")
    @Log(title = "单个审批接口", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping("/approveOne")
    public R<Object> approveOne(@RequestBody SysSubsidyMachineBo bo) {
        SysSubsidyMachineBo boo = iSysSubsidyMachineService.queryById(bo.getId());
        LoginUser loginUser = LoginHelper.getLoginUser();
        String key = loginUser.getRoles().get(0).getRoleKey();
        if ("districtNongji".equals(key)) {
            bo.setNode(4);
            bo.setStatus("已完成");
            bo.setNodeName("已完成");
        }
        if ("townNongji".equals(key)) {
            bo.setNode(3);
            bo.setStatus("审核中");
            bo.setNodeName("滴道区");
        }
        if ("common".equals(key)) {
            bo.setNode(2);
            bo.setStatus("审核中");
            bo.setNodeName(boo.getTownName());
        }

        // bo.setProccResult("驳回");
        // bo.setStatus("已处理");
        Boolean bo1 = iSysSubsidyMachineService.updateByMachineBo(bo);
        return R.ok();
    }


    /**
     * 申请农业补贴

     @SaCheckPermission("subsidy:subsidyApplication:add")
     @Log(title = "申请农业补贴", businessType = BusinessType.INSERT)
     @RepeatSubmit()
     @PostMapping("/submitnongye") public R<Object> submitnongji( @RequestBody SysSubsidyPlantingBo bo) {
     return R.ok((Object)iSysSubsidyMachineService.insertByBo(bo));
     }
     */


    /**
     * 修改农机补贴申请
     */
   // @SaCheckPermission("subsidy:subsidyApplication:edit")
    @Log(title = "农机补贴申请", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@RequestBody SysSubsidyMachineBo bo) {
        return toAjax(iSysSubsidyMachineService.updateByBo(bo));
    }

    /**
     * 删除农机补贴申请
     *
     * @param ids 主键串
     */
   // @SaCheckPermission("subsidy:subsidyApplication:remove")
    @Log(title = "补贴申请", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(iSysSubsidyMachineService.deleteWithValidByIds(Arrays.asList(ids), true));
    }
}
